﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace GPSTrackingWebsiteManager.Manager.Pages
{
    public partial class WindowsServiceLog : ClassManager.SessionManagerPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {            
            if (!Page.IsPostBack)
            {
                WindowsServiceGrid.DataSource = new System.Data.DataTable();
                WindowsServiceGrid.Height = Unit.Parse(HiddenFieldHeight.Value.ToString());
            }
        }

        protected void lbClearFilters_Click(object sender, EventArgs e)
        {
            WindowsServiceGrid.MasterTableView.FilterExpression = string.Empty;

            foreach (GridColumn column in WindowsServiceGrid.MasterTableView.RenderColumns)
            {
                if (column is GridBoundColumn)
                {
                    GridBoundColumn boundColumn = column as GridBoundColumn;
                    boundColumn.CurrentFilterValue = string.Empty;
                }
            }


            WindowsServiceGrid.MasterTableView.Rebind();
        }


        public void showErrorMessage(string msg)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "objWindowsMessageManager.show_miniError", "objWindowsMessageManager.show_miniError('" + msg + "','Windows Service Log ...');", true);
        }

        protected void lbShowError_Click(object sender, EventArgs e)
        {
            showErrorMessage(HiddenFieldError.Value);
        }

        protected void WindowsServiceGrid_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.FilterCommandName)
            {
                GPSTrackingDBV2.Data.WindowsServicesLogQuery windowsServicesLogQuery = new GPSTrackingDBV2.Data.WindowsServicesLogQuery();
                foreach (GridColumn column in WindowsServiceGrid.MasterTableView.RenderColumns)
                {
                    if (column is GridBoundColumn)
                    {
                        GridBoundColumn boundColumn = column as GridBoundColumn;

                        
                        if (!String.IsNullOrEmpty(boundColumn.CurrentFilterValue))
                        {
                            switch (boundColumn.DataField)
                            {
                                case "LogId":
                                    windowsServicesLogQuery.AppendEquals(GPSTrackingDBV2.Entities.WindowsServicesLogColumn.LogId, boundColumn.CurrentFilterValue);
                                    break;
                                case "DateTime":
                                    string[] Dates = boundColumn.CurrentFilterValue.Split(new Char[]{' '});
                                    windowsServicesLogQuery.AppendGreaterThanOrEqual(GPSTrackingDBV2.Entities.WindowsServicesLogColumn.DateTime, Dates[0]);
                                    windowsServicesLogQuery.AppendLessThan(GPSTrackingDBV2.Entities.WindowsServicesLogColumn.DateTime, Convert.ToDateTime(Dates[1]).AddDays(1).ToShortDateString());                                   
                                    break;
                                case "Category":
                                     windowsServicesLogQuery.AppendEquals(GPSTrackingDBV2.Entities.WindowsServicesLogColumn.Category, boundColumn.CurrentFilterValue);
                                    break;
                                case "Message":
                                    windowsServicesLogQuery.AppendContains(GPSTrackingDBV2.Entities.WindowsServicesLogColumn.Message, boundColumn.CurrentFilterValue);
                                    break;
                                case "GPSModel":
                                    windowsServicesLogQuery.AppendContains(GPSTrackingDBV2.Entities.WindowsServicesLogColumn.GpsModel, boundColumn.CurrentFilterValue);
                                    break;
                            }


                        }
                    }
                   
                }

                if (windowsServicesLogQuery.Length > 0)
                {
                    GPSTrackingDBV2.Entities.TList<GPSTrackingDBV2.Entities.WindowsServicesLog> WindowsServicesLogEntites = GPSTrackingDBV2.Data.DataRepository.WindowsServicesLogProvider.Find(windowsServicesLogQuery);
                    WindowsServiceGrid.DataSource = WindowsServicesLogEntites;                   
                }
                else
                {
                    WindowsServiceGrid.DataSource = new System.Data.DataTable();
                }
               
            }
        }

        protected void WindowsServiceGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            
        }
    }
}